function datastr = getBackscatterAvgSD_SE(filename)

%this is to remove the bad data and get the statistics for the backscatter


eval (['load ' filename '.mat'])
eval (['load ' filename 'index.asc'])
 
 i = eval ([ filename 'index'])

mncorr =70;


sizei=size(i)

if sizei(1)>=4
    ADV=[ A1(i(3):i(4)) A2(i(3):i(4)) A3(i(3):i(4)) C1(i(3):i(4)) C2(i(3):i(4)) C3(i(3):i(4))];
[p,q]=size(ADV)

good= ADV(:,4)>=mncorr & ADV(:,5)>=mncorr & ADV(:,6)>=mncorr;  % indices corresponding to all beams correlation greater than minimum
datagood=ADV(good,:);

gd=find(good==1);
nsmp=length(gd); 


ampmn=mean(datagood(:,1:3),2);       % determine mean backscatter amplitude from 3 beams
ampstd_rcd=mean(std(datagood(:,1:3)'))  %mean of the stadard deviation for the 3 beams





data(1,1)=mean(ampmn); %burst mean backscatter amplitude
data(1,2)=std(ampmn);  %burst standard deviation
data(1,3)=std(ampmn)/sqrt(nsmp); %burst standard error
data(1,4)=ampstd_rcd ;     %mean record stadard deviation
data(1,5)= nsmp;       %number of good samples
data(1,6)= p ;         %number of samples in the burst

data

clear ADV datagood gd nsmp ampmn ampstd_rcd p

end
pause;

if sizei(1)>=6
        ADV=[ A1(i(5):i(6)) A2(i(5):i(6)) A3(i(5):i(6)) C1(i(5):i(6)) C2(i(5):i(6)) C3(i(5):i(6))];
[p,q]=size(ADV)

good= ADV(:,4)>=mncorr & ADV(:,5)>=mncorr & ADV(:,6)>=mncorr;  % indices corresponding to all beams correlation greater than minimum
datagood=ADV(good,:);

gd=find(good==1);
nsmp=length(gd); 


ampmn=mean(datagood(:,1:3),2);       % determine mean backscatter amplitude from 3 beams
ampstd_rcd=mean(std(datagood(:,1:3)'))  %mean of the stadard deviation for the 3 beams





data(2,1)=mean(ampmn); %burst mean backscatter amplitude
data(2,2)=std(ampmn);  %burst standard deviation
data(2,3)=std(ampmn)/sqrt(nsmp); %burst standard error
data(2,4)=ampstd_rcd ;     %mean record stadard deviation
data(2,5)= nsmp;       %number of good samples
data(2,6)= p ;         %number of samples in the burst

data

clear ADV datagood gd nsmp ampmn ampstd_rcd p

pause;
end

if sizei(1)>=8
            ADV=[ A1(i(7):i(8)) A2(i(7):i(8)) A3(i(7):i(8)) C1(i(7):i(8)) C2(i(7):i(8)) C3(i(7):i(8))];
[p,q]=size(ADV)

good= ADV(:,4)>=mncorr & ADV(:,5)>=mncorr & ADV(:,6)>=mncorr;  % indices corresponding to all beams correlation greater than minimum
datagood=ADV(good,:);

gd=find(good==1);
nsmp=length(gd); 


ampmn=mean(datagood(:,1:3),2);       % determine mean backscatter amplitude from 3 beams
ampstd_rcd=mean(std(datagood(:,1:3)'))  %mean of the stadard deviation for the 3 beams





data(3,1)=mean(ampmn); %burst mean backscatter amplitude
data(3,2)=std(ampmn);  %burst standard deviation
data(3,3)=std(ampmn)/sqrt(nsmp); %burst standard error
data(3,4)=ampstd_rcd ;     %mean record stadard deviation
data(3,5)= nsmp;       %number of good samples
data(3,6)= p ;         %number of samples in the burst

data

clear ADV datagood gd nsmp ampmn ampstd_rcd p

pause;
end


eval(['save ' filename 'backscatter_stats.txt data -ascii'])


%end
